import { LineBreak } from "@/constant";
import { ICombinedJSON } from "@/types";
import { error, Ora } from '@/util';
import { writeFile } from "@/util/files";
import { generateTable } from "./sql";

function createSQL (combinedJSON: ICombinedJSON) {
    const ora = Ora('generate sql documents');
    try {
        const { configuration, table } = combinedJSON.database;
    
        let command = '';

        ora.start();
        table.forEach((tab) => {
            const { name, columns, needDefaultProps } = tab;
            command += `${generateTable(name, columns, needDefaultProps, configuration)}${LineBreak}`;
        });

        writeFile('wudongCli.sql.txt', command);
        ora.succeed();
    } catch(e) {
        error((e as string).toString());
        ora.fail();
    }
}

export {
    createSQL,
}